<template>
  <div>
    <a-form :form="regForm" :label-col="{ span: 4 }" :wrapper-col="{ span: 12 }">
      <a-form-item v-show="false">
        <a-input v-decorator="['configMd5']"/>
      </a-form-item>
      <a-form-item label="是否可注册">
        <a-radio-group v-decorator="['canReg',{rules: [{required: true,message:'请输入标题'}]}]">
          <a-radio :value="0">不可注册</a-radio>
          <a-radio :value="1">可注册</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="邮件主题">
        <a-input v-decorator="['subject']"/>
      </a-form-item>
      <a-form-item label="欢迎邮件模板" :wrapper-col="{ span: 18 }">
        <WangEditor :height="450" v-model="contentModel" source="DEFAULT" ref="editor" />
      </a-form-item>
      <a-form-item :wrapperCol="{offset:4}">
        <a-button type="primary" @click="handlerRegOk">保存</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script>
import { saveConfig, queryConfig } from '@/api/manage'
import { WangEditor } from '@/components'

export default {
  name: 'RegConfig',
  data () {
    return {
      regForm: this.$form.createForm(this)
    }
  },
  components: {
    WangEditor
  },
  methods: {
    initData () {
      queryConfig({
        configTypeEnum: 'WEB_CONFIG',
        configKeyEnum: 'REG',
        queryContent: true
      }).then(res => {
        if (res.data && res.data.length === 1) {
          this.$nextTick(() => {
            const data = JSON.parse(res.data[0].configContent)
            this.regForm.setFieldsValue({
              canReg: data.canReg,
              subject: data.subject,
              configMd5: res.data[0].configMd5
            })
            this.$refs.editor.setContent(data.content)
          })
        }
      })
    },
    handlerRegOk () {
      this.regForm.validateFields((errors, values) => {
        if (!errors) {
          var data = {
            configType: 'WEB_CONFIG',
            configKey: 'REG',
            configName: '注册配置',
            configContent: JSON.stringify({
              content: this.$refs.editor.getContent(),
              canReg: values.canReg,
              subject: values.subject
            }),
            configMd5: values.configMd5
          }
          saveConfig(data).then(res => {
            this.$message.info('保存成功')
          })
        }
      })
    }
  },
  activated () {
    this.initData()
  }
}
</script>
